#include <iostream>
#include <pthread.h>

class Mutex
{
public:
    Mutex(pthread_mutex_t *mutex = nullptr) : mutex_(mutex)
    {
        Lock();
    }
    void Lock()
    {
        if (mutex_)
        {
            pthread_mutex_lock(mutex_);   //如果mutex_不为空则表示已进行加锁操作
        }
    }
    void UnLock(){
        if(mutex_){
            pthread_mutex_unlock(mutex_);
        }
    }
    ~Mutex(){
        UnLock();
    }
private:
    pthread_mutex_t *mutex_;
};

class LockGuard
{
public:
    LockGuard(pthread_mutex_t *mutex): mutex_(mutex)
    {
        mutex_.Lock(); //在构造函数中进行加锁
    }
    ~LockGuard()
    {
        mutex_.UnLock(); //在析构函数中进行解锁
    }
private:
    Mutex mutex_;
};